package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.HttpServletBean;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/department")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    /**
     * 查询一个  localhost:8080/department/findone  1
     * localhost:8080/department/1
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "部门查找",notes = "根据ID查找部门")
    public Department findOne(@PathVariable("id") Long id){
        Department one = departmentService.findOne(id);
        return one;
    }

    /**
     * 查询所有localhost:8080/department
     * @return
     */
    @GetMapping
    @ApiOperation(value = "部门查找",notes = "查找所有部门")
    public List<Department> findAll(){
        List<Department> list = departmentService.findAll();
        return list;
    }
    @PutMapping  /*@RequestBody  接收请求体localhost:8080/department*/
    @ApiOperation(value = "添加或者修改部门",notes = "添加或者修改部门")
    public AjaxResult addorUpdate(@RequestBody Department department){
        try {
            if (department.getId()==null){//添加
                departmentService.add(department);
            }else{//修改
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    @DeleteMapping("/{id}")  /*@RequestBody  接收请求体localhost:8080/department/4*/
    @ApiOperation(value = "删除部门",notes = "根据ID删除部门")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败");
        }
    }

    @PatchMapping  /*@RequestBody  接收请求体localhost:8080/department*/
    @ApiOperation(value = "批量删除",notes = "根据ids删除部门")
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            departmentService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "分页查找部门",notes = "分页查找部门")/*localhost:8080/department*/
    public PageList<Department> queryForPage(@RequestBody DepartmentQuery departmentQuery, HttpServletRequest request){
        System.out.println("========当前登录人信息======="+LoginContext.getLoginInfo(request));
        PageList<Department> pageList = departmentService.queryForPage(departmentQuery);
        return pageList;
    }

    @GetMapping("/depttree")
    @ApiOperation(value = "查找上级部门",notes = "")/*localhost:8080/department/depttree*/
    public List<Department> getDeptTree(){
        List<Department> list = departmentService.getDeptTree();
        return list;
    }
}
